<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Port;//港口模型
use app\admin\model\Container_carrier;//承运商模型
use app\admin\model\Container;//货柜模型


class Logistics extends Auth
{
    //港口的列表
    public function port(Request $request)
    {
        $res = $request->param();
        $res['lang'] = $this->lang;
        if ($request->isAjax() && $request->isPost()) {
            $port = new Port();
            $getports = $port->getports($res);
            echo json_encode(array('getports' => $getports['data'], 'size' => $getports['count']));
        } elseif ($request->isGet()) {
            return view('port/port');
        }
    }

    //港口的添加
    public function portadd(Request $request)
    {
        $data = $request->param();
        $port = new Port();
        $country = getables('country');
        if ($request->isGet()) {
            return view('port/portinfo', [
                'str' => 'portadd',
                'country' => $country
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'portadd') {
            $portret = $port->addeditport($data);
            $udata["status"] = $portret['status'];
            $udata['msg'] = $portret['msg'];
            echo json_encode($udata);
        }
    }

    //港口的编辑
    public function portedit(Request $request)
    {
        $data = $request->param();
        $port = new Port();
        if ($request->isGet()) {
            $info = getinfo($data['id'], 'port');
            $country = getables('country');
            return view('port/portinfo', [
                'str' => 'portedit',
                'info' => $info,
                'country' => $country
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'portedit') {
            $portret = $port->addeditport($data);
            $udata["status"] = $portret['status'];
            $udata['msg'] = $portret['msg'];
            echo json_encode($udata);
        }

    }

    //港口的删除
    public function portdel(Request $request)
    {
        $data = $request->param();
        $port = new Port();
        if ($request->isPost() && $request->isAjax()) {
            $portret = $port->delport($data);
            $udata["status"] = $portret['status'];
            $udata['msg'] = $portret['msg'];
            echo json_encode($udata);
        }
    }

    //承运商列表
    public function carrier(Request $request)
    {
        $res = $request->param();
        if ($request->isAjax() && $request->isPost()) {
            $carrier = new Container_carrier();
            $getcarriers = $carrier->getcarriers($res);
            echo json_encode(array('getcarriers' => $getcarriers['data'], 'size' => $getcarriers['count']));
        } elseif ($request->isGet()) {
            return view('carrier/carrier');
        }
    }

    //承运商的添加
    public function carrieradd(Request $request)
    {
        $data = request()->param();
        $data['lang'] = $this->lang;
        $carrier = new Container_carrier();
        if ($request->isGet()) {
            return view('carrier/carrierinfo', [
                'str' => 'carrieradd',
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'carrieradd') {
            $carrierret = $carrier->addeditcarrier($data);
            $udata["status"] = $carrierret['status'];
            $udata['msg'] = $carrierret['msg'];
            echo json_encode($udata);
        }
    }

    //承运商的编辑
    public function carrieredit(Request $request)
    {
        $data = request()->param();
        $data['lang'] = $this->lang;
        $carrier = new Container_carrier();
        if ($request->isGet()) {
            return view('carrier/carrierinfo', [
                'str' => 'carrieredit',
                'id' => $data['id'],
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getcarrier'])) {
            //获取对应的语言的公司类型
            $where['id'] = $data['id'];
            $info = getlanginfo('container_carrier', $where);
            echo json_encode(array('info' => $info));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'carrieredit') {
            $carrierret = $carrier->addeditcarrier($data);
            $udata["status"] = $carrierret['status'];
            $udata['msg'] = $carrierret['msg'];
            echo json_encode($udata);
        }

    }

    //承运商的删除
    public function carrierdel(Request $request)
    {
        $carrier = new Container_carrier();
        $data = $request->param();
        if ($request->isPost() && $request->isAjax()) {
            $carrierret = $carrier->delcarrier($data);
            $udata["status"] = $carrierret['status'];
            $udata['msg'] = $carrierret['msg'];
            echo json_encode($udata);
        }
    }

    //货柜列表
    public function container(Request $request){
        $res=$request->param();
        $res['lang']=$this->lang;
        $container = new Container();
        if($request->isGet()){
            return view('container/container');
        }elseif ($request->isPost() && $request->isAjax()){
            //货柜列表的接口数据
            $getcontainers = $container->getcontainer($res);
            echo json_encode($getcontainers);
        }
    }
    //添加新货柜
    public function containeradd(Request $request){
        $container = new Container();
        $data=$request->param();
        $lang=$this->lang;
        if ($request->isGet()){
            //处理准备日期
            if ($lang != 'zh') {
                $dateday = getTimeDate(null, 13);
            } else {
                $dateday = getTimeDate(null, 7);
            }
            $datares=array(
                'dateday'=>$dateday,//日期
                'container_type'=>getables('container_type',[],'id,name'),//货柜类型
                'port_start'=>getables('port',['country_id'=>2],'id,name'),//出发地
                'port_arrival'=>getables('port',['country_id'=>1],'id,name'), //目标地
                'co_id_start'=>getables('container_agent',['country_id'=>2],'id,name'),// 货贷(中国)
                'co_id_arrival'=>getables('container_agent',['country_id'=>1],'id,name'),  //货贷(巴西)
                );
            return view('container/containeradd',[
               'data'=>$datares
            ]);
        }elseif ($request->isAjax() && $request->isPost()){
            //添加货柜的接口
            $containerret = $container->containeradd($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }
    //编辑货柜
    public function containeredit(Request $request){
        $data=$request->param();
        $lang=$this->lang;
        $container = new Container();
        if ($request->isGet()){
            $resdata=$container->editinfo($data,$lang);
            return view('container/containeredit',[
                'data'=>$resdata,
            ]);
        }elseif($request->isPost() && $request->isAjax()){
            $containerret = $container->containeredit($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }
    //批量调整状态
    public function containerchang(Request $request){
        $data=$request->param();
        $container = new Container();
        if ($request->isGet()){
           $lang=$this->lang;
           $resdata=$container->getchange($data,$lang);
           $ids=$data['ids'];
           return view('container/containerchange',[
                'resdata'=>$resdata,
               'ids'=>$ids,
           ]);
       }elseif ($request->isPost() && $request->isAjax()){
            $containerret = $container->containerchang($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }
    //处理货柜的打印
    public function containerdayi(Request $request){
        if ($request->isGet()){
            return view('container/containerdayi');
        }
    }
    //处理货柜详情查看
    public function containerdetail(Request $request){
       if($request->isGet()){
           return view('container/detail');
       }
    }
    //开航接口
    public function etdday(Request $request){
        $data=$request->param();
        $container = new Container();
        if ($request->isPost() && $request->isAjax()){
            $containerret = $container->etdetaarrivalday($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }
    //到达接口
    public function etaday(Request $request){
        $data=$request->param();
        $container = new Container();
        if ($request->isPost() && $request->isAjax()){
            $containerret = $container->etdetaarrivalday($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }
    //入仓接口
    public function arrivalday(Request $request){
        $data=$request->param();
        $container = new Container();
        if ($request->isPost() && $request->isAjax()){
            $containerret = $container->etdetaarrivalday($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }
    //货柜删除的接口
    public function containerdel(Request $request){
        $data=$request->param();
        $container = new Container();
        if ($request->isPost() && $request->isAjax()){
            $containerret = $container->delcontainer($data);
            $udata["status"] = $containerret['status'];
            $udata['msg'] = $containerret['msg'];
            echo json_encode($udata);
        }
    }
}